package com.fwzc.giuseppe.adapter;

import java.util.ArrayList;
import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.activity.ChangeActivity;
import com.fwzc.giuseppe.activity.CommentActivity;
import com.fwzc.giuseppe.activity.LogisticActivity;
import com.fwzc.giuseppe.activity.ProgressActivity;
import com.fwzc.giuseppe.activity.ReturnActivity;
import com.fwzc.giuseppe.entity.Goods;
import com.fwzc.giuseppe.util.OrderOperation;
import com.fwzc.giuseppe.util.OrderState;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("InflateParams")
public class AllOrderAdapter extends BaseAdapter {
	private Context mcontext;
	private ArrayList<Goods> datas;
	private LayoutInflater layoutInflater;
	private final int VIEWTYPE = 4;
	private final int TYPE_1 = OrderOperation.WAINT_PAYMENT;
	private final int TYPE_2 = OrderOperation.WAIT_RECEIVE;
	private final int TYPE_3 = OrderOperation.WAINT_COMMENT;
	private final int TYPE_4 = OrderOperation.CHANGE_RETURN;
	private int operation;
	private BitmapUtils bitmapUtil;
	private BitmapDisplayConfig config;
	
	public AllOrderAdapter(Context context, ArrayList<Goods> datas) {
		this.mcontext = context;
		this.datas = datas;
		layoutInflater = LayoutInflater.from(mcontext);
		bitmapUtil = new BitmapUtils(mcontext);
		config = new BitmapDisplayConfig();
		config.setLoadFailedDrawable(mcontext.getResources().getDrawable(R.drawable.error));
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		operation = datas.get(position).operation;
		return operation;
	}

	@Override
	public int getViewTypeCount() {
		return VIEWTYPE;
	}

	private class ViewHolderOne {
		ImageView clothes_image;
		TextView name;
		TextView code;
		TextView price;
		Button number;
		Button payment;
	}

	private class ViewHolderTwo {
		ImageView clothes_image;
		TextView name;
		TextView code;
		TextView price;
		Button number;
		Button sure;
		Button change;
		Button rejection;
		Button look;
	}

	private class ViewHolderThree {
		ImageView clothes_image;
		TextView name;
		TextView code;
		TextView price;
		Button number;
		Button comment;
	}

	private class ViewHolderFour {
		ImageView clothes_image;
		TextView name;
		TextView code;
		TextView price;
		Button number;
		Button progress;
		Button state_change;
		Button state_rejection;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolderOne holderOne = null;
		ViewHolderTwo holderTwo = null;
		ViewHolderThree holderThree = null;
		ViewHolderFour holderFour = null;
		int type = getItemViewType(position);
		final Goods order = datas.get(position);
		if (convertView == null) {
			switch (type) {
			case TYPE_1:
				holderOne = new ViewHolderOne();
				convertView = layoutInflater.inflate(R.layout.payment_list_item, null);
				holderOne.clothes_image = (ImageView)convertView.findViewById(R.id.clothes_image);
				holderOne.name = (TextView)convertView.findViewById(R.id.name);
				holderOne.code = (TextView)convertView.findViewById(R.id.order_code);
				holderOne.price = (TextView)convertView.findViewById(R.id.price);
				holderOne.number = (Button)convertView.findViewById(R.id.number);
				holderOne.payment = (Button)convertView.findViewById(R.id.payment);
				convertView.setTag(holderOne);
				break;

			case TYPE_2:
				holderTwo = new ViewHolderTwo();
				convertView = layoutInflater.inflate(R.layout.receive_list_item, null);
				holderTwo.clothes_image = (ImageView)convertView.findViewById(R.id.clothes_image);
				holderTwo.name = (TextView)convertView.findViewById(R.id.name);
				holderTwo.code = (TextView)convertView.findViewById(R.id.order_code);
				holderTwo.price = (TextView)convertView.findViewById(R.id.price);
				holderTwo.number = (Button)convertView.findViewById(R.id.number);
				holderTwo.sure = (Button)convertView.findViewById(R.id.sure);
				holderTwo.change = (Button)convertView.findViewById(R.id.change);
				holderTwo.rejection = (Button)convertView.findViewById(R.id.rejection);
				holderTwo.look = (Button)convertView.findViewById(R.id.look);
				convertView.setTag(holderTwo);
				break;

			case TYPE_3:
				holderThree = new ViewHolderThree();
				convertView = layoutInflater.inflate(R.layout.comment_list_item, null);
				holderThree.clothes_image = (ImageView)convertView.findViewById(R.id.clothes_image);
				holderThree.name = (TextView)convertView.findViewById(R.id.name);
				holderThree.code = (TextView)convertView.findViewById(R.id.order_code);
				holderThree.price = (TextView)convertView.findViewById(R.id.price);
				holderThree.number = (Button)convertView.findViewById(R.id.number);
				holderThree.comment = (Button)convertView.findViewById(R.id.comment);
				convertView.setTag(holderThree);
				break;

			case TYPE_4:
				holderFour = new ViewHolderFour();
				convertView = layoutInflater.inflate(R.layout.return_change_item, null);
				holderFour.clothes_image = (ImageView)convertView.findViewById(R.id.clothes_image);
				holderFour.name = (TextView)convertView.findViewById(R.id.name);
				holderFour.code = (TextView)convertView.findViewById(R.id.order_code);
				holderFour.price = (TextView)convertView.findViewById(R.id.price);
				holderFour.number = (Button)convertView.findViewById(R.id.number);
				holderFour.progress = (Button)convertView.findViewById(R.id.progress);
				holderFour.state_change = (Button)convertView.findViewById(R.id.state_change);
				holderFour.state_rejection = (Button)convertView.findViewById(R.id.state_rejection);
				convertView.setTag(holderFour);
				break;
			}
		} else {
			switch (type) {
			case TYPE_1:
				holderOne = (ViewHolderOne) convertView.getTag();
				break;

			case TYPE_2:
				holderTwo = (ViewHolderTwo) convertView.getTag();
				break;

			case TYPE_3:
				holderThree = (ViewHolderThree) convertView.getTag();
				break;

			case TYPE_4:
				holderFour = (ViewHolderFour) convertView.getTag();
				break;
			}
		}

		switch (type) {
		case TYPE_1:
			holderOne.name.setText(order.goods_name);
			holderOne.number.setText("x" + order.number);
			holderOne.price.setText("￥" + order.price);
			holderOne.code.setText("订单号:" + order.goods_code);
			bitmapUtil.display(holderOne.clothes_image, order.goods_url, config);
			holderOne.payment.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Toast.makeText(mcontext, "付款", Toast.LENGTH_SHORT).show();
				}
			});
			break;

		case TYPE_2:
			holderTwo.name.setText(order.goods_name);
			holderTwo.number.setText("x" + order.number);
			holderTwo.price.setText("￥" + order.price);
			holderTwo.code.setText("订单号:" + order.goods_code);
			bitmapUtil.display(holderTwo.clothes_image, order.goods_url, config);
			
			holderTwo.sure.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					((onSure) mcontext).click(datas.get(position).goods_id);
				}
			});
			holderTwo.change.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mcontext, ChangeActivity.class);
					intent.putExtra("suit_id", order.goods_id);
					intent.putExtra("brief", order.goods_name);
					intent.putExtra("orderNumber", order.goods_code);
					intent.putExtra("price", order.price);
					intent.putExtra("imgURL", order.goods_url);
					intent.putExtra("amount", order.number);
					mcontext.startActivity(intent);
				}
			});
			holderTwo.rejection.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mcontext, ReturnActivity.class);
					intent.putExtra("suit_id", order.goods_id);
					intent.putExtra("brief", order.goods_name);
					intent.putExtra("orderNumber", order.goods_code);
					intent.putExtra("price", order.price);
					intent.putExtra("imgURL", order.goods_url);
					intent.putExtra("amount", order.number);
					mcontext.startActivity(intent);
				}
			});
			holderTwo.look.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mcontext, LogisticActivity.class);
					intent.putExtra("suit_id", order.goods_id);
					intent.putExtra("brief", order.goods_name);
					intent.putExtra("orderNumber", order.goods_code);
					intent.putExtra("price", order.price);
					intent.putExtra("imgURL", order.goods_url);
					mcontext.startActivity(intent);
				}
			});
			break;

		case TYPE_3:
			holderThree.name.setText(order.goods_name);
			holderThree.number.setText("x"+order.number);
			holderThree.price.setText("￥" + order.price);
			holderThree.code.setText("订单号:" + order.goods_code);
			bitmapUtil.display(holderThree.clothes_image, order.goods_url, config);
			holderThree.comment.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mcontext,CommentActivity.class);
					intent.putExtra("suitId", order.goods_id);
					intent.putExtra("orderId", order.goods_code);
					mcontext.startActivity(intent);
				}
			});
			break;
			
		case TYPE_4:
			holderFour.name.setText(order.goods_name);
			holderFour.number.setText("x" + order.number);
			holderFour.price.setText("￥" + order.price);
			holderFour.code.setText("订单号:" + order.goods_code);
			if(order.state == OrderState.Return){
				holderFour.state_rejection.setVisibility(View.VISIBLE);
				holderFour.state_change.setVisibility(View.GONE);
			}else{
				holderFour.state_rejection.setVisibility(View.GONE);
				holderFour.state_change.setVisibility(View.VISIBLE);
			}
			bitmapUtil.display(holderFour.clothes_image, order.goods_url, config);
			holderFour.progress.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mcontext,ProgressActivity.class);
					intent.putExtra("suitId", order.goods_id);
					intent.putExtra("brief", order.goods_name);
					intent.putExtra("orderNumber", order.goods_code);
					intent.putExtra("amount", order.number);
					intent.putExtra("price", order.price);
					intent.putExtra("state", order.state);
					intent.putExtra("imgURL", order.goods_url);
					mcontext.startActivity(intent);
				}
			});
			break;
		}
		return convertView;
	}
	
	public interface onSure{
		void click(int suitId);
	}
}
